﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security;
using System.Security.Principal;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// 数据库操作
/// </summary>
public static class DB
{
    //数据库连接
    public static readonly string conn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
    /// <summary>
    /// 将密码进行MD5加密，并返回
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    public static String Encrypt(string password)
    {
        Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
        Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

        return BitConverter.ToString(hashedBytes);
    }
    
/// <summary>
/// 铭感字符串过滤 int 类型的。
/// </summary>
/// <param name="inputString">需要过滤的字符串</param>
/// <param name="maxLength">字符串要求总长度</param>
/// <returns>返回过滤后的字符串</returns>
    public static int ClearStringInput(string inputString,int maxLength)
    { 
        int id = 0;
        try
        {
            id = Convert.ToInt32(inputString);
            if (id.ToString().Length > maxLength)
            {
                System.Web.HttpContext.Current.Response.End();
            }
 
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.End();
        }
        
        return id;
    }
    public static int ClearStringInput(string inputString)
    {
        int id = 0;
        try
        {
            id = Convert.ToInt32(inputString);

        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.End();
        }

        return id;
    }
    /// <summary>
    /// 铭感字符串过滤 string 类型的。
    /// </summary>
    //public static string ClearStringInputSTR(string inputString, int maxLength)
    //{
    //    StringBuilder retVal = new StringBuilder();
    //    if ((inputString != null) && (inputString != String.Empty))
    //    {
    //        inputString = inputString.Trim();
    //        if (inputString.Length > maxLength)
    //        {
    //            inputString = inputString.Substring(0, maxLength);
    //        }
    //        for (int i = 0; i < inputString.Length; i++)
    //        {
    //            switch (inputString[i])
    //            {
    //                case '"': System.Web.HttpContext.Current.Response.End(); ; break;
    //                case '<': System.Web.HttpContext.Current.Response.End(); ; break;
    //                case '>': System.Web.HttpContext.Current.Response.End(); ; break;
    //                case ' ': System.Web.HttpContext.Current.Response.End(); ; break;
    //                case ';': System.Web.HttpContext.Current.Response.End(); ; break;
    //                case '=': System.Web.HttpContext.Current.Response.End(); ; break;
    //                case '!': System.Web.HttpContext.Current.Response.End(); ; break;
    //                default: retVal.Append(inputString[i]); break;
    //            }
    //        }
    //        retVal.Replace("'", "&#8217;");
    //    }
    //    return retVal.ToString();
    //}
    public static string ClearStringInputSTR(string qs,int maxLength)
    {
        if (qs.Length < maxLength)
        {
            if (!Z(qs.ToLower()))
            {
                HttpContext.Current.Response.Clear();
                DB.Alert1("您好，请不要输入铭感字符！");
                //HttpContext.Current.Response.End();
                return "";
            }
            else
            {
                return qs;
            }
        }
        else
        {
            DB.Alert1("输入字符超过限度！请修改！");
            return "";
        }

    }

    private static string[] al = ";|and|1=1|exec|insert|select|delete|update|like|count|chr|mid|master|or|truncate|char|declare|join".Split('|');
    private static bool Z(string v)
    {
        if (v.Trim() != "")
        {
            foreach (string a in al)
            {
                if (v.IndexOf(a + " ") > -1 || v.IndexOf(" " + a) > -1)
                {
                    return false;
                }
            }
        }
        return true;
    }
    /// <summary>
    /// 服务器端弹出alert对话框并返回
    /// </summary>
    /// <param name="str_Message">提示信息,例子："不能为空!"</param>
    public static void Alert(string str_Message)
    {
        HttpContext.Current.Response.Write("<script>alert('" + str_Message + " ');self.location=document.referrer</script>");
        HttpContext.Current.Response.End();
    }
    /// <summary>
    /// 服务器端弹出alert对话框并返回上一个页面
    /// </summary>
    /// <param name="str_Message">提示信息,例子："不能为空!"</param>
    public static void Alert1(string str_Message)
    {
        HttpContext.Current.Response.Write("<script>alert('" + str_Message + " ');history.go(-2)</script>");
        HttpContext.Current.Response.End();
    }
    /// <summary>
    /// 服务器端弹出alert对话框
    /// </summary>
    /// <param name="str_Message">提示信息,例子："不能为空!"</param>
    public static void Msg(string str_Message)
    {
      
        HttpContext.Current.Response.Write("<script>alert('" + str_Message + " ');window.location.href=window.location.href</script>");
    }
}
